package com.cndw.kungfu.model.battle;

import java.io.Serializable;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import com.cndw.kungfu.domain.GoodsMode;
import com.cndw.kungfu.domain.Player;
import com.cndw.kungfu.domain.SkillMode;
import com.cndw.kungfu.model.Context;
import com.cndw.kungfu.model.skill.PlayerSkill;
import com.cndw.kungfu.model.skill.SkillGird;
import com.cndw.kungfu.vo.battlevo.TeamPlayerVO;

/**
 * 跨服战斗对象
 * 
 * @author fantadust [9677889@qq.com]
 * @date 2012-7-27 下午9:05:46
 */
public class FighterCrossPlayer extends FighterBaseCross implements Fighter, ISkillGird, Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7435352088382462337L;
	public String dress;
	public int vocationId;
	
	private int score;

	public Collection<SkillGird> shortcutGirds;
	
	private Map<Integer, SkillGird> shortcutMap;
	
	public FighterCrossPlayer() {

	}

	public FighterCrossPlayer(Player player) {
		uniqueId = player.getPlayerId();
		name = player.getPlayerName();
		level = player.getLevel();
		hp = player.getHp();
		mp = player.getMp();
		maxHp = player.getMaxHp();// 最大体力
		maxMp = player.getMaxMp();// 最大魔法
		dress = player.getDress();

		attack = player.getAttack();// 物理攻击
		defense = player.getDefense();
		hit = player.getHit();
		dodge = player.getDodge();
		crit = player.getCrit();
		speed = player.getAttackSpeed();
		tough = player.getTough();

		hurtAdd = player.getHurtAdd();
		hurtRemove = player.getHurtRemove();
		hurtReflex = player.getHurtReflex();
		hurtAbsorb = player.getHurtAbsorb();
		critAdd = player.getCritAdd();
		
		vocationId = player.getVocationId();

		PlayerSkill playerSkill = Context.getSkillServer().getPlayerSkill(uniqueId);

		shortcutGirds = playerSkill.getShortcutGirds();
		score = player.getScore();

	}

	public String getDress() {
		return dress;
	}

	public void setDress(String dress) {
		this.dress = dress;
	}

	public int getVocationId() {
		return vocationId;
	}

	public void setVocationId(int vocationId) {
		this.vocationId = vocationId;
	}

	public TeamPlayerVO toVO() {
		return new TeamPlayerVO(this);
	}

	@Override
	public FighterBaseCross getSourceObject() {
		return this;
	}


	int lastGirdId = 0;
	
	Map<Integer, Integer> lastTimesMap = new HashMap<Integer, Integer>();
	
	private SkillGird getSkillGird(int nowstep, int sourceMp) {
		if (shortcutGirds == null || shortcutGirds.size() == 0 ) {
			return null;
		}
		if (shortcutMap == null) {
			shortcutMap = new HashMap<Integer, SkillGird>();
			for (SkillGird e : shortcutGirds) {
				shortcutMap.put(e.getPosition(), e);
			}
		}

		do {
			lastGirdId++;
			lastGirdId = lastGirdId > PlayerSkill.skillGirdMax ? 1 : lastGirdId;
		} while (!shortcutMap.containsKey(lastGirdId));

		SkillGird skillGird = shortcutMap.get(lastGirdId);
		if (skillGird == null) {
			return null;
		}
		int coolingTime = 0;
		int tmpGridId = skillGird.getId();
		if (skillGird.isAreGoods()) {	
			GoodsMode goodsMode = Context.getGoodsServer().getModeById(tmpGridId);
			if (goodsMode == null) {
				return null;
			}
			coolingTime = goodsMode.getCoolingTime();
		} else {
			SkillMode skillMode = Context.getSkillServer().getSkillMode(tmpGridId);
			if (skillMode == null || sourceMp < skillMode.getConstMp()) {
				return null;
			}
			coolingTime = skillMode.getCoolingTime();
		}
		if (coolingTime > 0) {
			int lastTime = 0;
			if (lastTimesMap.containsKey(tmpGridId)) {
				lastTime = lastTimesMap.get(tmpGridId);
			}
			if (lastTime == 0 || lastTime + coolingTime < nowstep) {
				lastTimesMap.put(tmpGridId, nowstep);
			} else {
				return null;
			}
		}
		if (skillGird != null) {
			
		}
		return skillGird;

	}

	@Override
	public SkillGird getSkillGird(int nowStep) {
		return getSkillGird(nowStep,mp);
	}

	@Override
	public int getScoreCHK() {
		return score;
	}
	
	public void resetSkillId(){
		lastGirdId = 0;
		lastTimesMap.clear();
	}

	public int getScore() {
		return score;
	}

	public void setScore(int score) {
		this.score = score;
	}

	@Override
	public int getHurtCoffAdd() {
		return 0;
	}

	@Override
	public int getHurtCoffRemove() {
		return 0;
	}
	
	

}
